from google.appengine.ext import db
from horseanalysis.lib.callable import Callable
from datetime import date, datetime, timedelta

class Race(db.Model):
  race_no = db.IntegerProperty()
  race_time = db.DateTimeProperty()
  venue = db.StringProperty()
  result = db.StringProperty()
  
  def display_race_time(self):
    return self.race_time.strftime("%d-%m-%Y %H:%M")
  
  def date_query(self):
    return self.race_time.strftime("%d-%m-%Y")
    
  def time_remaining(self):
    time_span = self.race_time - datetime.now()
    if time_span.days < 0:
      return ''
    else:
      if time_span.days == 1:
        return '%d day' % time_span.days
      elif time_span.days > 1:
        return '%d days ' % time_span.days
      else:
        return '%d mins ' % int(time_span.seconds / 60)
  
  def is_race_matched(self, race):
    if race.race_time == self.race_time:
      return True
    else:
      return False
  
  def set_result(self, race_result):
    self.result = race_result
    self.put()
  
  def get_first_three_results(self):
    if self.result:
      results = self.result.split(',')
      if len(results) >= 4:
        results = [results[0], results[1], results[2]]
      return results
    else:
      return []
  
  def is_existed(self):
    query = Race.gql("WHERE race_time = :1 and venue = :2 and race_no = :3", self.race_time, self.venue, self.race_no)
    return query.count() > 0
  
  def create_if_not_existed(race_no, race_time, venue):
    race = Race(race_no = race_no, race_time = race_time, venue = venue)
    if not race.is_existed():
      race.put()
      return True
    return False
    
  def get_races():
    return Race.gql("ORDER BY race_time desc")
  
  def get_current_race():
    query = Race.gql("WHERE race_time >= :1 ORDER BY race_time LIMIT 1", datetime.now())
    for race in query:
      return race
    return False
  
  def get_current_race_day_races():
    race = Race.get_current_race()
    if race:
      race_time = race.race_time
      start_time = datetime(race_time.year, race_time.month, race_time.day, 0, 0, 0)
      end_time = datetime(race_time.year, race_time.month, race_time.day, 23, 59, 59)
      query = Race.gql("WHERE race_time > :1 and race_time < :2 ORDER BY race_time", start_time, end_time)
      return query
    else:
      return False
  
  def get_race_needed_for_result():
    now = datetime.now()
    yesterday = now - timedelta(hours=6)
    return Race.gql("WHERE race_time > :1 AND race_time < :2 ", yesterday, now)
  
  def get_race(key_name):
    race = Race.get(key_name)
    return race
  
  def create_dummy_race():
    Race.create_new_race(1, datetime(year=2009, month=10, day=14, hour=11, second=0, minute=15), 'HV')
    Race.create_new_race(2, datetime(year=2009, month=10, day=14, hour=11, second=0, minute=45), 'HV')
    Race.create_new_race(3, datetime(year=2009, month=10, day=14, hour=12, second=0, minute=15), 'HV')
    Race.create_new_race(4, datetime(year=2009, month=10, day=14, hour=12, second=0, minute=45), 'HV')
    Race.create_new_race(5, datetime(year=2009, month=10, day=14, hour=13, second=0, minute=15), 'HV')
    Race.create_new_race(6, datetime(year=2009, month=10, day=14, hour=13, second=0, minute=45), 'HV')
    Race.create_new_race(7, datetime(year=2009, month=10, day=14, hour=14, second=0, minute=15), 'HV')
    Race.create_new_race(8, datetime(year=2009, month=10, day=14, hour=14, second=0, minute=50), 'HV')
    
  def create_new_race(race_no, race_time, venue, result = None):
    race = Race(race_no=race_no, race_time=race_time, venue=venue, result=result)
    race.put()
  
  def create_sample_data():
    pass
  
  create_dummy_race = Callable(create_dummy_race)
  create_new_race = Callable(create_new_race)
  get_current_race = Callable(get_current_race)
  get_current_race_day_races = Callable(get_current_race_day_races)
  get_race_needed_for_result = Callable(get_race_needed_for_result)
  get_race = Callable(get_race)
  get_races = Callable(get_races)
  create_sample_data = Callable(create_sample_data)
  create_if_not_existed = Callable(create_if_not_existed)